
import { useState, useEffect } from 'react';

import styles from './index.module.css'


import 'react-pdf/dist/Page/AnnotationLayer.css';
import 'react-pdf/dist/Page/TextLayer.css';
import { Document, Page, pdfjs } from 'react-pdf';
pdfjs.GlobalWorkerOptions.workerSrc = new URL(
    'pdfjs-dist/build/pdf.worker.min.mjs',
    import.meta.url,
).toString();



function Preview() {
    const [numPages, setNumPages] = useState(null);
    const [pageNumber, setPageNumber] = useState(1);

    // 示例PDF文件（使用一个公开可用的PDFURL）
    // const pdfUrl = 'http://localhost:9010/000_000_001';
    const pdfUrl = 'http://localhost:9010/preview/pdf/c87c64b007694c3485645c98d448045d.pdf'

    function onDocumentLoadSuccess({ numPages }) {
        setNumPages(numPages);
    }

    function handlePrint() {
        // window.print('http://localhost:9010/preview/pdf/c87c64b007694c3485645c98d448045d.pdf');

        const pdfWindow = window.open('http://localhost:9010/preview/pdf/c87c64b007694c3485645c98d448045d.pdf', '_blank');
        // pdfWindow.onload = () => {
        //     console.log(pdfWindow);
        //     pdfWindow.print();
        //     pdfWindow.close(); // 打印后关闭窗口（如果需要）
        // };

        // setTimeout(() => {
        //     pdfWindow.close();
        // }, 10000);


    }

    return (
        <div>
            <Document
                file={pdfUrl}
                onLoadSuccess={onDocumentLoadSuccess}
                loading={<div>正在加载PDF...</div>}
                error={<div>加载PDF失败</div>}>
                <Page
                    pageNumber={1}
                    width={800}
                />
            </Document>
            <iframe
                title="printFrame"
                name="printFrame"
                src='http://localhost:9010/preview/pdf/c87c64b007694c3485645c98d448045d.pdf'
                style={{ width: '100%', height: '600px', display: 'none' }} // 隐藏iframe
            />
            <button onClick={handlePrint} >
                打印文档
            </button>
        </div>
    )
}

export default Preview;